﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool;

using FluentNHibernate.Cfg;
using FluentNHibernate;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;

using TCEasyPrintRev.Models;


namespace TCEasyPrintRev.Controllers
{
    public class NHibernateHelper
    {
        public static ISessionFactory sessionFactory;

        const string connString = "Server=10.151.34.78;Database=tceasyprint;User ID=coba;Password=123;Trusted_Connection=False";
        public static ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                    CreateSessionFactory1();
                return sessionFactory;
            }
        }

        public static void InitializeSessionFactory()
        {
            sessionFactory = Fluently.Configure().
                Database(MsSqlConfiguration.MsSql2008.
                ConnectionString(connString).
                ShowSql()).Mappings(m => m.FluentMappings.AddFromAssemblyOf<TCEasyPrintRev.Models.Cetak>()).
                ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false ,false))
                .BuildSessionFactory();
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

        public static ISessionFactory GetSessionFactory()
        {
            return Fluently.Configure().
                Database(MsSqlConfiguration.MsSql2008.
                ConnectionString(connString).
                ShowSql()).Mappings(m => m.FluentMappings.AddFromAssemblyOf<TCEasyPrintRev.Models.Cetak>())
                 .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
                .BuildSessionFactory();
        }

        public static void CreateSessionFactory1()
        {
            sessionFactory =
                Fluently.Configure().
                Database(MsSqlConfiguration.MsSql2008.
                ConnectionString(connString).
                ShowSql()).Mappings(m => m.FluentMappings.AddFromAssemblyOf<TCEasyPrintRev.Models.Cetak>())
                 .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
                .BuildSessionFactory();
        }
    }
}